import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import routes from './routes';
import {abandonOrder} from './api';
const {Description} = DescriptionList;
import {injectIntl} from './intl';
export class DetailPanel extends React.PureComponent {
    componentDidMount() {
        this.props.init();
    }

    onClickAbandonBtn = () => {
        this.setState({loading: true});
        abandonOrder(this.props.detail.id, this.props.detail.rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }

    render() {
        const {formatMessage} = this.props.intl;
        return (
            <div className="detail">
                <Card loading={this.props.loading}>
                    <DescriptionList size="large" title={<Row><Col span={6}>{formatMessage({
                        id: 'detailPanel.text.title',
                        defaultMessage: '基本信息'
                    })}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.code',
                            defaultMessage: '编号'
                        })}>{this.props.detail.code}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.name',
                            defaultMessage: '名称'
                        })}>{this.props.detail.name}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.brandCode',
                            defaultMessage: '品牌'
                        })}>{`(${this.props.detail.brandCode})/${this.props.detail.brandName}`}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.areaName',
                            defaultMessage: '国际区域'
                        })}>{this.props.detail.areaName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.address',
                            defaultMessage: '地址'
                        })}>{this.props.detail.address}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.email',
                            defaultMessage: '电子邮箱'
                        })}>{this.props.detail.email}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.remark',
                            defaultMessage: '备注'
                        })}>{this.props.detail.remark}</Description>
                    </DescriptionList>
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.props.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPage.text.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    detail: PropTypes.object.isRequired,
    id: PropTypes.string.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    getDetail,
    tableSearch
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);


const mapStateToProps = (state, props) => ({
    id: props.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    detail: getDetailData(state),
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
